package com.chalmers.irc2you;

import java.util.LinkedList;
import java.util.Timer;
import java.util.TimerTask;

import com.chalmers.irc2you.model.Message;
import com.chalmers.irc2you.network.Connection;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.view.inputmethod.EditorInfo;

public class Irc2YouNotificationManager extends Service {

	private NotificationManager notificationManager;
	private int YOUR_NOTIFICATION_APPID;
	private Context context;
	private Connection connection;
	private Timer timer = new Timer();
	private long timerDelay = 600000L;
	private int YOUR_APPID;

	/**
	 * This method shows a new notification in the status bar with the specified
	 * parameters
	 * 
	 * @param timeStamp
	 *            the time of the hilight
	 * @param sender
	 *            the user that hilighted you
	 * @param message
	 *            the message that hilighted you
	 * @param channel
	 *            the channel in which you where hilighted
	 */
	public void showNotification(String timeStamp, String sender,
			String message, String channel) {

		Intent contentIntent = new Intent(context, MainActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 1,
				contentIntent, PendingIntent.FLAG_ONE_SHOT);

		// Convert the ticker_text to a charsequence.
		CharSequence csTicker_text = context.getString(R.string.one_hilight);

		// Create Notification.
		Notification notification = new Notification(R.drawable.irc2you,
				csTicker_text, System.currentTimeMillis());
		notification
				.setLatestEventInfo(context, sender, message, pendingIntent);
		notification.flags = 16;

		// Notify the NotificationManager of the new notification.
		notificationManager.notify(YOUR_NOTIFICATION_APPID, notification);
	}

	public void showNotification(int i) {

		Intent contentIntent = new Intent(context, MainActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 1,
				contentIntent, PendingIntent.FLAG_ONE_SHOT);

		// Convert the ticker_text to a charsequence.
		CharSequence csTicker_text = context
				.getString(R.string.multiple_hilights);

		// Create Notification.
		Notification notification = new Notification(R.drawable.irc2you,
				csTicker_text, System.currentTimeMillis());
		notification.setLatestEventInfo(context, "Multiple new hilights",
				"Click to get to Irc2You", pendingIntent);
		notification.flags = 16;

		// Notify the NotificationManager of the new notification.
		notificationManager.notify(YOUR_NOTIFICATION_APPID, notification);
	}

	public void failedNotification() {
		Intent contentIntent = new Intent(context, MainActivity.class);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 1,
				contentIntent, PendingIntent.FLAG_ONE_SHOT);

		// Convert the ticker_text to a charsequence.
		CharSequence csTicker_text = context
				.getString(R.string.failed_to_get_hilights);

		// Create Notification.
		Notification notification = new Notification(R.drawable.irc2you,
				csTicker_text, System.currentTimeMillis());
		notification.setLatestEventInfo(context, "Failed to update hilights",
				"Click to get to Irc2You", pendingIntent);
		notification.flags = 16;

		// Notify the NotificationManager of the new notification.
		notificationManager.notify(YOUR_NOTIFICATION_APPID, notification);
	}

	private void updateHilights() {
		Log.i("irc2you", "Trying to get Hilights");
		LinkedList<Message> hilights = Connection.getHilights();
		if (hilights.size() == 0) {
			failedNotification();
		} else if (hilights.size() <= 1) {
			Message msg = hilights.get(0);
			showNotification(msg.getTimeStamp(), msg.getSender(), msg
					.getMessage(), msg.getChannel());
		} else {
			showNotification(hilights.size());
		}
	}

	@Override
	public IBinder onBind(Intent arg0) {
		onStartCommand();
		return null;
	}

	public void onStartCommand() {
		context = getApplicationContext();
		YOUR_APPID = (int) (Math.random() * 42);

		
		TimerTask task = new TimerTask() {

			@Override
			public void run() {
				updateHilights();
			}
		};
		timer.schedule(task, 0L, timerDelay);
		updateHilights();
	}
	
	public void onDestroy(){
		timer.cancel();
		timer.purge();
	}
}
